﻿# -*- coding:utf-8 -*-
import datetime
import os
import random
import re

import pika


class Demo(object):

    def get_data(self):
        return '<?xml version="1.0" encoding="UTF-8"?><WP_PLU02_EXT><IDOC BEGIN="1"><E1WPA01 SEGMENT="1"><FILIALE></FILIALE><AENDKENNZ></AENDKENNZ><AKTIVDATUM></AKTIVDATUM><AENDDATUM></AENDDATUM><HAUPTEAN><![CDATA[748843434]]></HAUPTEAN><ARTIKELNR><![CDATA[10006437]]></ARTIKELNR><POSME></POSME><E1WPA02 SEGMENT="E1WPA02"><WARENGR><![CDATA[0103]]></WARENGR><ARTIKANZ></ARTIKANZ><MHDHB><![CDATA[90]]></MHDHB></E1WPA02><E1WPA03 SEGMENT="1"><QUALARTTXT><![CDATA[LTXT]]></QUALARTTXT><TEXT><![CDATA[鲁西黄牛牛腿肉20g]]></TEXT></E1WPA03><E1WPA04 SEGMENT="1"><KONDART></KONDART><FreeFlag><![CDATA[1]]></FreeFlag><BEGINDATUM><![CDATA[20220419]]></BEGINDATUM><BEGINNZEIT><![CDATA[20220419000000]]></BEGINNZEIT><ENDDATUM><![CDATA[20220419]]></ENDDATUM><ENDZEIT><![CDATA[20220419000000]]></ENDZEIT><E1WPA05 SEGMENT="1"><VORZEICHEN></VORZEICHEN><KONDWERT><![CDATA[1]]></KONDWERT><MENGE></MENGE><CURRENCY></CURRENCY></E1WPA05></E1WPA04><E1WPA07 SEGMENT="1"><MWSKZ><![CDATA[0]]></MWSKZ><ZTAXCLS><![CDATA[1010101010000000000]]></ZTAXCLS></E1WPA07><E1WXX01 SEGMENT=""><FLDNAME>ZMINQTY</FLDNAME><FLDVAL><![CDATA[0]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZINVDAY</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>	<E1WXX01 SEGMENT="1"><FLDNAME>ZRECSHLFLIF</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>	<E1WXX01 SEGMENT=""><FLDNAME>ZSALSHLFLIF</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>		<E1WXX01 SEGMENT=""><FLDNAME>ZPRSLOC</FLDNAME><FLDVAL><![CDATA[0001]]></FLDVAL></E1WXX01>								<E1WXX01 SEGMENT=""><FLDNAME>ZORDTYP</FLDNAME><FLDVAL><![CDATA[1]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT="1"><FLDNAME>ZFRFDCD1</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZFRFDCD2</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT="1"><FLDNAME>ZFRFDCD3</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT="1"><FLDNAME>ZORDDATUM</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZRORDATUM</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZSALETIME</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>		<E1WXX01 SEGMENT=""><FLDNAME>ZSALBLKETI</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>	<E1WXX01 SEGMENT=""><FLDNAME>ZPARCOUP</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT="1"><FLDNAME>ZMAXQTY</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>		<E1WXX01 SEGMENT=""><FLDNAME>ZCORFLAG</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>				<E1WXX01 SEGMENT=""><FLDNAME>ZSHCOUNT</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>	<E1WXX01 SEGMENT="1"><FLDNAME>ZSTCKALL</FLDNAME><FLDVAL><![CDATA[Y]]></FLDVAL></E1WXX01>	<E1WXX01 SEGMENT="1"><FLDNAME>ZBWSCL</FLDNAME><FLDVAL><![CDATA[1]]></FLDVAL></E1WXX01>	<E1WXX01 SEGMENT="1"><FLDNAME>ZARTTYPE</FLDNAME><FLDVAL><![CDATA[Z001]]></FLDVAL></E1WXX01>									<E1WXX01 SEGMENT="1"><FLDNAME>ZEKGRP</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>	<E1WXX01 SEGMENT=""><FLDNAME>ZGRWPOALL</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>					<E1WXX01 SEGMENT="1"><FLDNAME>ZLFRHY</FLDNAME><FLDVAL><![CDATA[S00]]></FLDVAL></E1WXX01><E1WXX01 SEGMENT=""><FLDNAME>ZMAABC</FLDNAME><FLDVAL><![CDATA[B]]></FLDVAL></E1WXX01>		<E1WXX01 SEGMENT=""><FLDNAME>ZVENDLT</FLDNAME><FLDVAL><![CDATA[]]></FLDVAL></E1WXX01>	<E1WXX01 SEGMENT=""><FLDNAME>SSTOCK</FLDNAME><FLDVAL><![CDATA[0.000]]></FLDVAL></E1WXX01>																					<E1WXX01 SEGMENT=""><FLDNAME>PROMSG</FLDNAME><FLDVAL></FLDVAL></E1WXX01><E1WXX01 SEGMENT="1"><FLDNAME>ZSIFLAG</FLDNAME><FLDVAL></FLDVAL></E1WXX01><E1WXX01 SEGMENT="1"><FLDNAME>ZPRFLAG</FLDNAME><FLDVAL></FLDVAL></E1WXX01></E1WPA01></IDOC></WP_PLU02_EXT>'

    def get_MDM_data(self):
        pass

    def change_data(self):
        index = random.randint(100000000000000000, 999999999999999999)
        # 商品条码
        HAUPTEAN = re.findall(r'<HAUPTEAN><!\[CDATA\[(.*?)\]\]></HAUPTEAN>', self.get_data())[0]
        # 商品ID
        ARTIKELNR = re.findall(r'<ARTIKELNR><!\[CDATA\[(.*?)\]\]></ARTIKELNR>', self.get_data())[0]
        # 商品名称
        TEXT = re.findall(r'<TEXT><!\[CDATA\[(.*?)\]\]></TEXT>', self.get_data())[0]
        new_str = self.get_data().replace(HAUPTEAN, str(index)).replace(ARTIKELNR, str(index)).replace(TEXT, str(TEXT) + str(index))
        print(index)
        return new_str

    def send_mq(self, message):
        connection = pika.BlockingConnection(pika.ConnectionParameters(host='10.224.24.107', port=5672, credentials=pika.PlainCredentials('admin', 'aldi123456')))
        channel = connection.channel()
        channel.queue_declare(queue='mq.wso2.to.ims', durable=True)
        channel.basic_publish(exchange='', routing_key='mq.wso2.to.ims', body=message)
        connection.close()

    def write_file(self, str):
        pass


if __name__ == '__main__':
    demo = Demo()
    for (index, i) in enumerate(range(1000)):
        print(datetime.datetime.today().strftime("%Y%m%D-%H:%M:%S") + "----------" + str(index))
        new_str = demo.change_data()
        demo.send_mq(new_str)